﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace newTurtle8
{
    public class Initialization
    {
        CompanyManager companyManager;
        SqlManager sqlManager;
        public Initialization(SqlManager sqlManager)
        {
            companyManager = CompanyManager.getInstance();
            this.sqlManager = sqlManager;

            setDBServerPath();
            initializeDBTables();
            setBackupPath();
        }

        private void setDBServerPath()
        {
            string s = System.Environment.MachineName;
            modifyInitFile("DBServer", "=", "=" + s + @"\SQLEXPRESS");
        }

        private void setBackupPath()
        {
            //retreiving the path of MSSQL in file system
            string selectStr = SqlManager.getSqlStringFromFile(@"\other\getInstallationPath.sql");
            SqlCommand selectCmd = SqlManager.SqlCommandFactory(selectStr);
            SqlDataReader reader = SqlManager.getSqlDataReader(selectCmd);

            reader.Read();
            string backupPath = (string)reader[0] + @"\Backup";

            selectCmd.Connection.Close();
            reader.Close();

            modifyInitFile("BackupPath", "=", "=" + backupPath);
        }

        private void modifyInitFile(string key, string oldVal, string newVal)
        {
            // create reader & open file
            TextReader tr = new StreamReader(@"other\company.ini");
            TextWriter tw = new StreamWriter(@"other\company-temp.ini");
            string line;

            while ((line = tr.ReadLine()) != null)
            {
                if (line.StartsWith("<" + key))
                    line = line.Replace(oldVal, newVal);
                tw.WriteLine(line);
            }

            tr.Close();
            tw.Close();

            File.Delete(@"other\company.ini");
            File.Move(@"other\company-temp.ini", @"other\company.ini");
            File.Delete(@"other\company-temp.ini");
        }

        private void initializeDBTables()
        {
            sqlManager.initConnection(true);

            SqlCommand createDbCmd = SqlManager.SqlCommandFactory("CREATE DATABASE companyManager");
            SqlManager.executeNonQuery(createDbCmd);
            SqlManager.Connection.Close();

            sqlManager.initConnection(false);

            try
            {
                List<string> files = new List<string>();
                files.AddRange(Directory.GetFiles(CompanyManager.StartupPath + @"/sql/CREATE"));

                foreach (string file in files)
                    SqlManager.executeNonQuery(SqlManager.SqlCommandFactory(SqlManager.getSqlStringFromFile(file)));

                files.Clear();
                files.AddRange(Directory.GetFiles(CompanyManager.StartupPath + @"/sql/StoredProcedures"));
                foreach (string file in files)
                    SqlManager.executeNonQuery(SqlManager.SqlCommandFactory(SqlManager.getSqlStringFromFile(file)));

                //populateEnglishHebrewDictionaryTable();
                //populateIsraelCitiesTable();
                modifyInitFile("IsInitialized", "false", "true");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
